package com.le.tester.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * createTime：2025-04-12 15:33
 * description：atomikos数据源配置
 */

@Configuration
public class AtomikosDataSourceConfig {


    @Value("${spring.employee-datasource.jdbc-url}")
    public String empDBUrl;

    @Value("${spring.employee-datasource.username}")
    public String empUser;

    @Value("${spring.employee-datasource.password}")
    public String empPass;

    //salary
    @Value("${spring.salary-datasource.jdbc-url}")
    public String salaryDBUrl;

    @Value("${spring.salary-datasource.username}")
    public String salaryUser;

    @Value("${spring.salary-datasource.password}")
    public String salaryPass;

    @Bean(name = "employeeDataSource")
    public DataSource employeeDataSource() {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("employeeDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");

        //连接属性
        Properties properties = new Properties();
        properties.setProperty("URL", empDBUrl);
        properties.setProperty("user", empUser);
        properties.setProperty("password", empPass);
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;
    }

    @Bean(name = "salaryDataSource")
    public DataSource salaryDataSource() {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("salaryDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");

        //连接属性
        Properties properties = new Properties();
        properties.setProperty("URL", salaryDBUrl);
        properties.setProperty("user", salaryUser);
        properties.setProperty("password", salaryPass);
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;
    }

}
